package com.estronger.shopmanage.ui.order.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.order.bean.OrderAttributeBean;

import java.util.List;

/**
 * Created by DingYG on 2017-07-28.
 */

public class SelectActionAdapter extends BaseAdapter {

    private List<OrderAttributeBean> mList;
    Activity mContext;
    boolean showInMiddle;//是否居中显示
    private String checkedId;//选中的id
    private int redColor;//选中颜色
    private int norColor;//未选中颜色

    public SelectActionAdapter(Activity context, List<OrderAttributeBean> lt) {
        mContext = context;
        mList = lt;
        showInMiddle = false;
        redColor = mContext.getResources().getColor(R.color.red);
        norColor = mContext.getResources().getColor(R.color.unselect_color);
    }

    public void setShowInMiddle(boolean showInMiddle) {
        this.showInMiddle = showInMiddle;
    }

    public void setCheckedId(String checkedId) {
        this.checkedId = checkedId;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public OrderAttributeBean getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHoder vh;
        final OrderAttributeBean att = mList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_select_action, null);
            vh = new ViewHoder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHoder) convertView.getTag();
        }
        try {
            if(showInMiddle){
                vh.tvfTextLeft.setVisibility(View.GONE);
                vh.tvfTextMiddle.setVisibility(View.VISIBLE);
                vh.tvfTextMiddle.setText(att.getAttName());
                if(checkedId != null && checkedId.equals(att.getAttId())) {
                    vh.tvfTextMiddle.setTextColor(redColor);
                }else {
                    vh.tvfTextMiddle.setTextColor(norColor);
                }
            }else{
                vh.tvfTextMiddle.setVisibility(View.GONE);
                vh.tvfTextLeft.setVisibility(View.VISIBLE);
                vh.tvfTextLeft.setText(att.getAttName());
                if(checkedId != null && checkedId.equals(att.getAttId())) {
                    vh.tvfTextLeft.setTextColor(redColor);
                }else {
                    vh.tvfTextLeft.setTextColor(norColor);
                }
            }
            if(position == mList.size()-1){
                vh.ivfGed.setVisibility(View.GONE);
            }else{
                vh.ivfGed.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {}
        return convertView;
    }

    public class ViewHoder {
        TextView tvfTextLeft;
        TextView tvfTextMiddle;
        RelativeLayout rlfselpadt;
        ImageView ivfGed;

        public ViewHoder(View view) {
            this.tvfTextLeft = (TextView) view.findViewById(R.id.tvnameLeft);
            this.tvfTextMiddle = (TextView) view.findViewById(R.id.tvnameMiddle);
            this.rlfselpadt = (RelativeLayout) view.findViewById(R.id.order_selpadt);
            this.ivfGed = (ImageView) view.findViewById(R.id.ivfGed);
        }
    }
}